﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunSpace.Application.SiteMgr;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Domain.ViewModel.SiteMgr;

namespace YunSpace.Web.Areas.ClientManager.Controllers
{
    public class ClientInfosController : ControllerBase
    {
        //
        // GET: /ClientManager/ClientInfos/
        ClientInfosApp app = new ClientInfosApp();

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult AddProject()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult AddMemand()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult EditForm()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult EditDemandForm()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult MyClientsManager()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetGridData(Pagination pagination, string queryJson)
        {            
            var data = new
            {
                rows = app.GetClientsList(pagination, queryJson, ""),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetGridDataByUser(Pagination pagination, string queryJson)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var data = new
            {
                rows = app.GetMyClientsList(pagination, queryJson, LoginInfo.UserId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetGridProjectData(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = app.GetClientProjectInfoList(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }       

        [HttpGet]
        public ActionResult GetGridServiceData(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = app.GetClientServiceInfoList(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetClientHistoryOrderList(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = app.GetClientHistoryOrderList(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetProjectInfo(string F_Id)
        {
            var data = app.GetProjectInfo(F_Id);
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetProjectList(string F_ClientId)
        {
            var data = app.GetProjectList(F_ClientId);
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetClientEntity(string F_ClientId)
        {
            var data = app.GetClientEntity(F_ClientId);
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetProjectInfoList(Pagination pagination, string F_ClientId,string F_UserId)
        {
            var data = new
            {
                rows = app.GetProjectInfoList(pagination, F_ClientId, F_UserId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetItemListJson()
        {
            //var citys = app.GetCitys();
            var data = new
            {
                F_ClientType = app.GetItemList("khlx"),
                F_CompanyType = app.GetItemList("gslx"),
                F_ClientLevel = app.GetItemList("khdj"),
                F_IndustryId = app.GetItemList("sshy"),
                F_DepartmentId = app.GetItemList("bmmc"),
                F_PositionId = app.GetItemList("gszw"),
                F_ActivityType = app.GetItemList("ActivityType"),
                First_ActivityType = app.GetItemList("FirstActivityType"),
                //F_ActivityCityId = app.GetAllCitys(),
                F_SiteType = app.GetItemList("SiteType"),
                F_CityId = app.GetAllCitys(),
                F_AreaId = app.GetAllAreas(),
                SiteClients = app.GetClients(),
                Brands = app.GetBrands(),
                Contacts = app.GetClientsContacts(),
                Projects = app.GetClientsAllProjects()
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetAllCitys() {
            var result = app.GetAllCitys();
            return Content(result.ToJson());
        }

        [HttpGet]
        public ActionResult GetItems()
        {
            var data = new
            {
                F_CompanyType = app.GetItemList("gslx"),
                F_IndustryId = app.GetItemList("sshy"),
                F_ClientType = app.GetItemList("khlx"),
                F_ClientLevel = app.GetItemList("khdj"),
                F_CityId = app.GetAllCitys(),
                F_AreaId = app.GetAllAreas(),
                F_ActivityType = app.GetItemList("ActivityType"),
                First_ActivityType = app.GetItemList("FirstActivityType"),
                F_SiteType = app.GetItemList("SiteType"),
                SiteClients = app.GetClients(),
                Users = app.GetUsers()
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetDemandItems()
        {
            var data = new
            {
                F_SiteType = app.GetItemList("SiteType"),
                F_ActivityCityId = app.GetAllCitys(),
                F_IndustryId = app.GetItemList("sshy"),
                F_ActivityType = app.GetItemList("ActivityType"),
                First_ActivityType = app.GetItemList("FirstActivityType"),
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetProjectInfoDetail(string DemandId)
        {
            var data = app.GetProjectInfoDetail(DemandId);
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult GetContactInfo(string F_ContactId)
        {
            var data = app.GetContactInfo(F_ContactId);
            return Content(data.ToJson());
        }

        /// 提交表单
        /// </summary>
        /// <param name="siteEntity">表单实体</param>
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SubmitForm(ClientTotalActionModel entity, string keyValue)
        {
            var result= app.SubmitForm(entity, keyValue);
            return Content(result.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitProjectData(ClientProjectAddActionModel entity)
        {
            app.SubmitProjectData(entity);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitDemandData(ClientDemandActionModel entity)
        {
            app.SubmitDemandData(entity);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        //[ValidateAntiForgeryToken]
        public ActionResult SubmitProjectForm(ClientProjectUpdateActionModel entity)
        {            
            var result= app.SubmitProjectForm(entity);
            return Content(result.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitCompanyForm(ClientCompanyActionModel entity)
        {
            app.SubmitCompanyForm(entity);
            return Success("操作成功。");
        }

        #region 我的审核

        /// <summary>
        /// 我的审核页
        /// </summary>
        /// <returns></returns>
        public ActionResult MyAudit()
        {
            return View();
        }

        /// <summary>
        /// 我的审核页网格数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMyAuditGridJson(Pagination pagination, string orderId)
        {
            object data = app.GetMyAuditList(pagination, orderId);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 我的审核详情页
        /// </summary>
        /// <returns></returns>
        public ActionResult MyAuditDetail()
        {
            return View();
        }

        /// <summary>
        /// 我的审核详情页数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMyAuditDetailJson()
        {
            object data = "";
            return Content(data.ToJson());
        }

        /// <summary>
        /// 审核订单档期
        /// </summary>
        /// <returns></returns>
        public ActionResult AuditOrderState(int state, string siteClientOrderRecommendId)
        {
            app.UpdateOrderRecommendState(state, siteClientOrderRecommendId);
            return Success("操作成功。");
        }

        #endregion 我的审核

        #region 处理历史数据
        SalasApp salesapp = new SalasApp();
        [HttpGet]
        public ActionResult GetSalaList(Pagination pagination)
        {
            var data = new
            {
                rows = salesapp.GetList(),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        
        [HttpPost]
        public ActionResult OperationSalas() {
            salesapp.OperationSalas();
            return Success("操作成功。");
        }
        #endregion
    }
}
